//
//  XDTeacherMgrViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDTeacherMgrViewController.h"
#import "HNRelativeModel.h"

@interface XDTeacherMgrViewController ()<UITextFieldDelegate>
{
    IBOutletCollection(UIView) NSArray *Viiews;
    IBOutlet UIButton *_SendCode;
    IBOutlet UITableView *_TableView;
    IBOutlet UITextField *_Accout;
    IBOutlet UITextField *_NickName;
    IBOutlet UITextField *_IdCode;
    IBOutlet UIButton *_GetCode;
}
@property (nonatomic, assign) NSInteger TimeIndex;
@end

@implementation XDTeacherMgrViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.frame = CGRectMake(0, 44, ScreenW, ScreenH - 44 - 64);
    
    
    
    _TableView.tableFooterView = [UIView new];
    _TableView.tableHeaderView = [UIView new];
    
    //view样式
    for (UIView *View in Viiews)
    {
        [self SetLayer:View];
    }
    
    _Accout.placeholder = [NSString stringWithFormat:@"请输入%@的帐号",self.title];
    _NickName.placeholder = [NSString stringWithFormat:@"请输入%@的昵称",self.title];
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnKeyboard)]];
}





#pragma mark - 配置
/** 样式更正 */
- (void)SetLayer:(UIView *)View
{
    View.layer.cornerRadius = 5.0;
    View.layer.masksToBounds = YES;
    View.layer.borderColor = [ThemeColor CGColor];
    View.layer.borderWidth = 1.0f;
}



#pragma mark - Action

/**
 发送验证码
 */
- (IBAction)SendCode:(UIButton *)sender
{
    
    [self returnKeyboard];
    //判断有效手机号
    NSString *message = [LLTools valiMobile:_Accout.text];
    if(message.length != 0){
        [MBProgressHUD showError:message];
        return;
    }
    
    [MBProgressHUD showMessage:@"正在发送验证码"];
    
    [XDHttpTool get:BaseURL(@"qun.do?action=starapkurl") params:@{@"phone":_Accout.text} success:^(id responseObj) {
        
        NSLog(@"验证码 -- %@",responseObj);
        self.TimeIndex = 61;
        sender.userInteractionEnabled = NO;
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(MgrButtonClick:) userInfo:nil repeats:YES];
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD showError:@"验证码发送失败"];
        NSLog(@"%@",error);
    }];
    
}


/** 添加 */
- (IBAction)Add:(UIButton *)sender
{
    if (
        _Accout.text.length     <= 0 ||
        _NickName.text.length   <= 0 ||
        _IdCode.text.length     <= 0
        )
    {
        [MBProgressHUD showError:@"请完善相关信息"];
    }
    else
    {
        [MBProgressHUD showHUD];
        NSDictionary *Dict = @{
                               @"phone"     :_Accout.text,
                               @"nickname"  :_NickName.text,
                               @"qid"       :[[self.DataArr firstObject] qid],
                               @"rolecode"  :[self.title isEqualToString:@"老师"]?@"2":@"3"
                               };
        [XDHttpTool get:BaseURL(@"qun.do?action=addquser") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
            
            if (responseObj)
            {
            }
            else
            {
            }
        } failure:^(NSError *error) {
            [MBProgressHUD showError:XDNetWorkError];
        }];
    }
}

#pragma mark - Function
/** 禁用验证码的点击 */
- (void)MgrButtonClick:(NSTimer *)Timer
{
    self.TimeIndex -= 1;
    [_GetCode setTitle:[NSString stringWithFormat:@"(%ld)",self.TimeIndex] forState:(UIControlStateNormal)];
    
    if (self.TimeIndex == 0)
    {
        [Timer invalidate];
        [_GetCode setTitle:@"发送验证码邀请对方加入" forState:(UIControlStateNormal)];
        _GetCode.userInteractionEnabled = YES;
    }
}

/**
 return 键盘
 */
-(void)returnKeyboard{
    
    [self.view endEditing:YES];
}

#pragma mark - TextField代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self returnKeyboard];
    return YES;
}
@end
